package pure.engine.world 
{
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	import pure.engine.core.IFrameListener;
	import pure.engine.core.ns_despair;
	import pure.engine.model.Model2D;
	import pure.engine.core.ProcessManager;
	import pure.engine.Multiple;
	
	use namespace ns_despair;
	
	/**
	 * 监视器
	 * [Author] - PURE
	 * 
	 * [Hint] - 最后的输出渲染，纯粹的view，没有任何对其的实际控制.
	 * 
	 */
public class Monitor extends Sprite implements IMonitor, IFrameListener
{	
	public function Monitor()
	{
		this.mouseChildren = this.mouseEnabled = false;
	}
	
	
	
	//======================
	// Const
	//======================
	
	
	ns_despair static const w_exitingFlag:uint   =  0x01;  // 退出中
	ns_despair static const w_modifiedFlag:uint  =  0x02;  // 更改
	
	
	
	
	/**
	 * 显示
	 */
	public function show(owner:DisplayObjectContainer, x:int, y:int):void
	{
		owner.addChild(this);
		this.x = x;
		this.y = y;
	}
	
	
	/**
	 * 隐藏
	 */
	public function hide():void
	{
		if (parent)
		{
			this.parent.removeChild(this);
		}
	}
	
	
	/**
	 * 初期化
	 */
	ns_despair function initialize(bodyName:String, viewW:int, viewH:int):void
	{
		this.m_name         =  bodyName;
		this.m_viewW        =  viewW;
		this.m_viewH        =  viewH
		this.m_layerFusion  =  new LayerFusion();
		//this.m_scrollRect   =  new Rectangle(0, 0, m_viewW, m_viewH);
		//this.scrollRect     =  this.m_scrollRect;
		this.addChild(this.m_layerFusion);
		
		ProcessManager.instance.addFrameListener(this, ProcessManager.MONITOR);
	}
	
	
	/**
	 * 更新
	 *           
	 *  [Feature] -     WorldBody       Monitor
	 * 
	 *             1.     Ready         Exiting
	 *             2.     Advance       Modified
	 *             3.     Enter          
	 * 
	 */
	public function update(deltaTime:Number):void
	{
		var modelList:Vector.<Model2D>;
		var l:int;
		
		if (Boolean(m_flags & w_modifiedFlag))
		{
			// 新入残像
			l = m_modifiedModelList.length;
			while (--l > -1)
			{
				m_layerFusion.makeMirrorToLayer(m_modifiedModelList[l]);
			}
			m_modifiedModelList.length = 0;
			
			// 图层刷新
			this.m_layerFusion.flush();
		}
		else if(Boolean(m_flags & w_exitingFlag))
		{
			_exit();
		}
	}

	
	/**
	 * 退出的过程...可扩展.
	 */
	protected function _exit():void
	{
		WorldManager.finishExit(m_name);
		
		
	}
	

	/**
	 * 进入世界
	 */
	ns_despair function enterWorld():void
	{
		m_flags = w_modifiedFlag;
	}
	
	
	/**
	 * 退出世界
	 */
	ns_despair function exitWorld():void
	{
		m_layerFusion.clear();
		m_flags = w_exitingFlag;
	}
	
	
	/**
	 * 完成退出
	 */
	ns_despair function finishExit():void
	{
		this.m_flags = 0;
	}
	
	

	
	/* ----------------------------------------------------------------------------------------------------------------
	 * ★★★★★★★★★★★★★★★★★★★★★  private  ★★★★★★★★★★★★★★★★★★★★★
	 * ----------------------------------------------------------------------------------------------------------------
	 */
	
	
	
	
	//======================
	// Member
	//======================

	
	ns_despair var m_name:String;  // 名字
	
	ns_despair var m_layerFusion:LayerFusion;  // 图层融合
	
	ns_despair var m_modifiedModelList:Array;
	
	ns_despair var m_scrollRect:Rectangle;  // 显示区域
	
	ns_despair var m_flags:int;
	
	ns_despair var m_viewW:int;
	
	ns_despair var m_viewH:int;
}

}